﻿Imports System.Activities
Imports System.ComponentModel
Imports System.Activities.Presentation.PropertyEditing
Imports System.Collections.ObjectModel
Imports System.Activities.Presentation.Metadata
Imports System.Drawing

Public Class GetFolderListActivity
    Inherits AsyncCodeActivity
    Implements IRegisterMetadata

    Public Property Pattern As InArgument(Of String)
    Public Property TargetFolder As InArgument(Of String)
    Public Property Result As OutArgument(Of String())

    Private Delegate Function asyncGetFolderListDelegate(ByVal target As String, ByVal pattern As String) As String()

    Public Sub New()
        Me.DisplayName = My.Resources.GetFolderListDisplayName
    End Sub

    Protected Overrides Function BeginExecute(ByVal context As System.Activities.AsyncCodeActivityContext, ByVal callback As System.AsyncCallback, ByVal state As Object) As System.IAsyncResult
        Dim targetPattern = context.GetValue(Me.Pattern)
        Dim targetFodler = context.GetValue(Me.TargetFolder)
        Dim asyncExecute = New asyncGetFolderListDelegate(AddressOf GetFolderList)
        context.UserState = asyncExecute

        Return asyncExecute.BeginInvoke(targetFodler, targetPattern, callback, state)
    End Function

    Protected Overrides Sub EndExecute(ByVal context As System.Activities.AsyncCodeActivityContext, ByVal result As System.IAsyncResult)
        Dim asyncExecute = TryCast(context.UserState, asyncGetFolderListDelegate)
        Dim getResult = asyncExecute.EndInvoke(result)

        context.SetValue(Me.Result, getResult)
    End Sub

    Private Function GetFolderList(ByVal target As String, ByVal pattern As String) As String()

        Dim resultList As String() = Nothing
        Try
            If String.IsNullOrEmpty(pattern.Trim) Then
                resultList = My.Computer.FileSystem.GetDirectories(target).ToArray
            Else
                resultList = My.Computer.FileSystem.GetDirectories(target, FileIO.SearchOption.SearchTopLevelOnly, pattern).ToArray
            End If
        Catch ex As Exception

        End Try
        Return resultList
    End Function

    Public Sub Register() Implements System.Activities.Presentation.Metadata.IRegisterMetadata.Register
        Dim build As New AttributeTableBuilder
        'FolderCreate Activity
        build.AddCustomAttributes(GetType(GetFolderListActivity),
                                              New ToolboxBitmapAttribute(GetType(GetFolderListActivity), "GetFolderList.png"))
        build.AddCustomAttributes(GetType(GetFolderListActivity), "Pattern",
                                              New CategoryAttribute(My.Resources.GetFolderListCategory),
                                              New DisplayNameAttribute(My.Resources.GetFolderListTargetDisplay),
                                              New DescriptionAttribute(My.Resources.GetFolderListTargetDescription))
        build.AddCustomAttributes(GetType(GetFolderListActivity), "TargetFolder",
                                              New RequiredArgumentAttribute,
                                              New EditorAttribute(GetType(FolderpathPropertyEditor), GetType(DialogPropertyValueEditor)),
                                              New CategoryAttribute(My.Resources.GetFolderListCategory),
                                              New DisplayNameAttribute(My.Resources.GetFolderListTargetFolderDisplay),
                                              New DescriptionAttribute(My.Resources.GetFolderListTargetFolderDescription))
        build.AddCustomAttributes(GetType(GetFolderListActivity), "Result",
                                              New CategoryAttribute(My.Resources.GetFolderListCategory),
                                              New DisplayNameAttribute(My.Resources.GetFolderListResultDisplay),
                                              New DescriptionAttribute(My.Resources.GetFolderListResultDescription))

        MetadataStore.AddAttributeTable(build.CreateTable)

    End Sub

End Class
